package com.hhq.core;

import java.io.*;
import java.util.*;

public class Config {

    
    public static String ROOT_PATH ;  // 类路径
    private static Properties pro;   // 配置键值对
    // 静态文件夹目录
    public static String STATIC_PATH ;
    public static String[] VIEW_PATH;   // 视图类

    public static String HOST = "127.0.0.1";   
    public static int PORT = 8000;
    public static int num = 128;   // 同时连接数量
    public static String[] befores;
    public static String[] afters;

    // 类的静态初始化
    static{
        ROOT_PATH = Config.class.getResource("/").getPath().substring(1) ; 
        System.out.println(ROOT_PATH); // F:/AllCode/java/httpServer/target/classes/
        pro =  getProperties();
        if(pro != null){
            try {
                // 获取启动服务器的ip、端口、最大连接数量、静态文件地址、视图类的位置等
                HOST= pro.getProperty("host", "127.0.0.1").trim();
                PORT= Integer.parseInt( pro.getProperty("port", "8000").trim() );
                num= Integer.parseInt( pro.getProperty("num", "128").trim() );
                STATIC_PATH= pro.getProperty("static-path", ROOT_PATH+"static" ).trim();
                if (pro.getProperty("view-path") == null){
                    System.err.println("配置文件未写明视图类的路径，例如:view-path=com.hhq.View");
                    throw new NoClassDefFoundError("配置文件未写明视图类的路径");
                }
                VIEW_PATH = spliteByColon(pro.getProperty("view-path")) ;

                // 获取中间件
                befores=spliteByColon(pro.getProperty("before"));
                afters=spliteByColon(pro.getProperty("after"));

            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("配置文件错误!");
                System.exit(-1);
            }
        }
    }

    // mimeType
    public static Map<String, String> mineMaps = new TreeMap<>();

    static{
        mineMaps.put("gif", "image/gif");
        mineMaps.put("bmp", "image/bmp");
        mineMaps.put("ico", "image/x-ico");
        mineMaps.put("jpeg", "image/jpeg");
        mineMaps.put("jpg", "image/jpeg");
        mineMaps.put("png", "image/png");
        mineMaps.put("zip", "application/zip");
        mineMaps.put("rar", "application/x-rar");
        mineMaps.put("jsp", "text/html");
        mineMaps.put("pdf", "application/pdf");
        mineMaps.put("png", "image/png");
        mineMaps.put("jpg", "image/jpeg");
        mineMaps.put("mp4", "video/mp4");
        mineMaps.put("flv", "video/x-flv");
        mineMaps.put("ppt", "application/vnd.ms-powerpoint");
        mineMaps.put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
        mineMaps.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        mineMaps.put("xls", "application/vnd.ms-excel");
        mineMaps.put("doc", "application/msword");
        mineMaps.put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        mineMaps.put("txt", "text/plain");
        mineMaps.put("html", "text/html");
        mineMaps.put("css", "text/css");
        mineMaps.put("js", "application/javascript");

    }

    // 获取配置 hhqconfig.properties 信息
    private static Properties getProperties(){
        try {
            FileInputStream fis=new FileInputStream(ROOT_PATH + "hhqconfig.properties");
            ByteArrayOutputStream bb=new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int len=0;
            while(  (len = fis.read(buf))  != -1)  {
                bb.write(buf, 0, len);
            }
            fis.close();
            String res = bb.toString("UTF-8");
            res = res.replace("\\", "/");
            Properties pro = new Properties();
            pro.load(new StringReader(res));
            System.out.println(pro);
            return pro;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("读取配置文件 hhqconfig.properties 出错!");
            System.exit(-1);
        }
        return null;
    }

    // 通过冒号：分割字符串，返回 数组
    private static String[] spliteByColon(String line){
        if(line ==null || line.trim().length() == 0 ){
            return new String[]{};
        }
        String[] args = line.split(":");
        ArrayList<String> res=new ArrayList<>();
        for(String arg : args){
            String a = arg.trim();
            if(a.length() != 0){
                res.add(a);
            }
        }
        String[] r = new String[res.size()];
        res.toArray(r);
        return r;
    } 

}